<?php

class AclController extends Zend_Controller_Action
{
    public function init()
    {
        parent::init();
        $this->view->title = 'Access Control List';
    }
    
    public function indexAction()
    {
        $resources  = Tri_Config::get('resources', true);
        $roles      = Tri_Config::get('roles', true);
        $lista      = array();
        foreach($roles as $v){ $lista[$v]=$v;}
        $this->view->data = $resources;
        $this->view->roles = $lista;
    }

    public function saveAction()
    {
        Tri_Config::set('resources', $_POST['data'], true);
        
        $this->_helper->flashMessenger->addMessage('Success');
        
        $this->_redirect('default/acl');
    }

    public function addAction()
    {
        $url  = explode('/', trim($this->_getParam('url')));
        $role = trim($this->_getParam('role'));

        if (count($url) != 3 || !$role) {
            $this->_helper->flashMessenger->addMessage('Error');
            $this->_redirect('default/acl');
        }

        $resources = Tri_Config::get('resources', true);

        $resources[$url[0]][$url[1]][$url[2]] = $role;

        Tri_Config::set('resources', $resources, true);
        
        $this->_helper->flashMessenger->addMessage('Success');
        $this->_redirect('default/acl');
    }
}
